#include	"unp.h"
#include "var.h"
#include "var1.h"

int listenFTPServConn() {

	int listenfd, connfd;
	socklen_t clilen;
	struct sockaddr_in cliaddr, servaddr;


	listenfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(PASSIVE_PORT_NUM - 1);

	const int on = 1;
	Setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
	Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

	Listen(listenfd, LISTENQ);

	connfd = Accept(listenfd, (SA*) &cliaddr, &clilen);

	return connfd;

	//Close(connfd);

}

char *getMyAddr(int socket) {

	struct sockaddr_in saaddr;
	int len = sizeof(saaddr);
	getsockname(socket, (struct sockaddr *) &saaddr, &len);
	//return sockstr.sa_data + 2;
	return inet_ntoa(saaddr.sin_addr);
}

void makeActiveMsg(char *IPAddr,char *activeMsg) {

	int i = 0;
	char temp[WORD_LENG];
	snprintf(temp, WORD_LENG, "%s", IPAddr);

	for (i = 0; i < WORD_LENG; i++) {
		if (temp[i] == 0)
			break;
		if (temp[i] == '.')
			temp[i] = ',';
	}
	//fprintf(stderr, "%s", temp);

	snprintf(activeMsg, WORD_LENG, "PORT %s,%d,%d\r\n", temp,
			PASSIVE_PORT_NUM / 256, PASSIVE_PORT_NUM % 256);
	//fprintf(stderr, "%s", passiveMsg);
	PASSIVE_PORT_NUM++;


}

int makeClientCommand(char *buf, char *command) {

	int wordN;
	Word word[WORD_NUM];

	wordN = sscanf(buf, "%s %s", word[0].word, word[1].word);

	if ((strstr(buf, "open") != NULL) || (strstr(buf, "user") != NULL)) {
		return OPEN;
	}

	else if ((strstr(buf, "ls") != NULL) || (strstr(buf, "dir") != NULL)
			|| (strstr(buf, "nlist") != NULL)) {
		if (wordN == 1) {
			snprintf(command, MAXLINE, "LIST\r\n");
		} else if (wordN == 2) {
			snprintf(command, MAXLINE, "LIST %s\r\n", word[1].word);
		}
		return LS;
	} else if (strstr(buf, "pwd") != NULL) {
		snprintf(command, MAXLINE, "PWD\r\n");
		return PWD;
	} else if ((strstr(buf, "cd") != NULL)) {
		snprintf(command, MAXLINE, "CWD %s\r\n", word[1].word);
		return CD;
	} else if (strstr(buf, "lcd") != NULL) {
		return LCD;
	}

	else if ((strstr(buf, "type") != NULL)) { // only typing type
		if (wordN == 1) {
			return TYPE;
		} else if (wordN == 2) {
			if (strcmp(word[1].word, "binay") == 0) {
				snprintf(command, MAXLINE, "TYPE B\r\n");
				BINARY_MODE = ON;
			} else if (strcmp(word[1].word, "ascii") == 0) {
				snprintf(command, MAXLINE, "TYPE A\r\n");
				BINARY_MODE = OFF;
			}
			return TYPE_CHANGE;
		}
	} else if (strstr(buf, "ascii") != NULL) {
		snprintf(command, MAXLINE, "TYPE A\r\n");
		BINARY_MODE = OFF;
		return TYPE_CHANGE;
	}
	else if (strstr(buf, "binary") != NULL) {
		snprintf(command, MAXLINE, "TYPE B\r\n");
		BINARY_MODE = ON;
		return TYPE_CHANGE;
	} else if (strstr(buf, "get") != NULL) {
		snprintf(command, MAXLINE, "RETR %s\r\n", word[1].word);
		snprintf(PATHNAME, MAXLINE, "%s", word[1].word);
		return GET;
	} else if (strstr(buf, "put") != NULL) {
		snprintf(command, MAXLINE, "STOR %s\r\n", word[1].word);
		snprintf(PATHNAME, MAXLINE, "%s", word[1].word);
		return PUT;
	} else if (strstr(buf, "close") != NULL) {
		snprintf(command, MAXLINE, "QUIT\r\n", word[1].word);
		return CLOSE;
	} else if ((strstr(buf, "quit") != NULL) || (strstr(buf, "bye") != NULL)) {
		snprintf(command, MAXLINE, "QUIT\r\n", word[1].word);
		return QUIT;
	} else if (strstr(buf, "system") != NULL) {
		snprintf(command, MAXLINE, "SYST\r\n");
		return SYSTEM;
	}	else if (strstr(buf, "passive") != NULL) {
		return PASSIVE;
	} else if (strstr(buf, "debug") != NULL) {
		return DEBUG;
	}

	else {
		return ELSE;
	}

}

void debug(char *command) {
	if(DEBUG_MODE == OFF) {
		return;
	}else if(DEBUG_MODE == ON) {
		fprintf(stderr,"---> %s",command);
	}
}

